-- UIRelicUserInfo
-- Create By Cheny3 Dec/28/2015
-- 掠夺信息界面

-- UIRelicUserInfo继承自Layer
UIRelicUserInfo = class("UIRelicUserInfo", function()
    return cc.Layer:create();
end);

function UIRelicUserInfo.create(info)
    return UIRelicUserInfo.new(info);
end

-- 内部函数声明
local offsetY = 0;

-- 奖励格最大行数和列数
local MAX_GRID_ROW    = 2;
local MIN_GRID_COLUMN = 4;
local SCRAP_CLASS_ID  = 10000;

-- 构造函数
function UIRelicUserInfo:ctor(info)
    -- 初始化
    self:setName("UIRelicUserInfo");
    -- 复用飞艇信息页面
    local node = cc.CSLoader:createNode("layout/sky_city/IslandInfo.csb");
    self:addChild(node);
    self.node = node;

    -- 传递的信息
    self.info = info;

    self:getDetailInfo();

    local bonusNode = findChildByName(self.node, "BT2/desc_node/bonus_node");
    self.scrollView = findChildByName(bonusNode, "bonus_panel");

    -- 记录自己的位置信息
    self.myPos = SkyRelicM.getRelicElementPos(self.info[1], self.info[2]);

    local titleLabel = findChildByName(self.node, "Top/title/text");
    self.titleLabelX = titleLabel:getPositionX();

    -- 注册事件处理回调函数
    self:registerEventCallback();

    local towerNode = findChildByName(self.node, "CT/tower_node");
    self.towerNodePosY = towerNode:getPositionY();

    -- 重绘
    self:redraw();

    -- 播放信息出现效果
    local detailNode = findChildByName(self.node, "CT/detail_node");
    playAirShipInfoEffect(detailNode);

    -- 播放准心特效
    -- local reticleNode = findChildByName(self.node, "CT/reticle_node");
    playReticleEffect(self.node);

    -- 注册点击事件
    self:registerTouchEvent();

    -- 标题和描述在中间浮现
    self:comformCenterEffect();

    -- 适配
    self:resize();
end

-- 初始化奖励格子
function UIRelicUserInfo:initGrids(gridCount)
    local w = DESIGN_WIDTH;
    local h = DESIGN_HEIGHT;

    -- 图标间距(横向)
    local wGap = -5;

    -- 图标间距(纵向)
    local hGap = 10;

    -- 图标宽度
    local iconWidth = 127;

    -- 图片高度
    local iconHeight = 110;

    local topHGap  = 10;
    local bottomHGap  = 0;
    local leftWGap = 5;

    -- 总数
    local totalCount = gridCount;

    self.scrollView.rows = {};
    self.scrollView:removeAllChildren();

    local itemNode = cc.CSLoader:createNode("layout/sky_city/SkyStoreItem.csb");
    -- @param windowNode    界面节点
    -- @param scrollNode    容器节点
    -- @param itemNode      单元格的节点
    -- @param totalNum      单元格总数
    -- @param maxRowNum     最大行数
    -- @param pageColNum    单页可以显示下的列数
    -- @param wGap          单元格间距(横向) 这里作为最小值使用
    -- @param hGap          单元格间距(纵向)
    -- @param iconWidth     单元格宽度
    -- @param iconHeight    单元格高度
    -- @param topHGap       上方留白
    -- @param leftWGap      左右留白  这里作为最小值使用
    -- @param touchEvent    点击事件注册函数
    -- @param initHandle    格子初始化委托
    -- @param schedulerId   定时器id
    initScrollGridsVertical(self, self.scrollView, itemNode,
        totalCount, MAX_GRID_ROW, MIN_GRID_COLUMN, wGap, hGap,
        iconWidth, iconHeight, topHGap, bottomHGap, leftWGap);

    for _, child in ipairs(self.scrollView.rows) do
        child:setScale(0.88);
    end
end

-- 清空所有的奖励格
function UIRelicUserInfo:clearAllGrid()
    self.scrollView.rows = {};
    self.scrollView:removeAllChildren();
end

-- 重绘奖励
function UIRelicUserInfo:redrawBonus()
    -- 收集信息
    local goodsList = self.data["bonus"] or {};
    local totalGridCount = #goodsList;

    if not goodsList or #goodsList == 0 then
        cclog("收集信息失败。");
        self:clearAllGrid();
        return;
    end

    if #goodsList < 4 then
        totalGridCount = 4;
    end
    -- 初始化格子
    self:initGrids(totalGridCount);
    local totalGoodsCount = #goodsList;

    for i = 1, totalGridCount do
        local grid          = self.scrollView:getChildByName("item" .. i);
        local goodsBg       = grid:getChildByName("bg");
        local buyImg        = grid:getChildByName("buy");
        local starNode      = grid:getChildByName("stars");
        local numLabel      = grid:getChildByName("num_label");
        local countLabel    = grid:getChildByName("num");
        local iconImg       = grid:getChildByName("icon");

        countLabel:setVisible(true);
        numLabel:setVisible(false);
        buyImg:setVisible(false);

        if i <= totalGoodsCount and type(goodsList[i]) == "table" then
            grid:setVisible(true);

            local classId = goodsList[i][2];
            local count   = goodsList[i][3];

            -- 显示数量
            TextStyleM.setTextStyle(countLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, -2);
            if count > 1 then
                -- 需要显示数量
                countLabel:setString(changeNumType(count));
            else
                countLabel:setVisible(false);
            end

            -- 显示商品图标
            local goodsIcon;
            local para;
            if goodsList[i][1] == 1 then
                goodsIcon = getItemIconPath(ItemM.query(classId, "icon"));
                if self.info[1] == "relic" and classId == SCRAP_CLASS_ID then
                    -- 遗迹区分矿车中掠夺的和额外掠夺的
                    local nameStr = ItemM.query(classId, "name");
                    local detail;
                    if i == 1 then
                        nameStr = spliceLocStr(nameStr, getLocStr("relic_rob_scrap1"));
                        -- 矿车中的图标
                        goodsIcon = getRelicIcon("car_scrap");
                        detail = getLocStr("car_scrap_detail");
                    else
                        nameStr = spliceLocStr(nameStr, getLocStr("relic_rob_scrap2"));
                        local airshipInfo = NewAirShipM.getUserAirShipInfo(NewAirShipM.getActiveAirShip());
                        -- 己方幸运
                        local sourceLuck = airshipInfo.luck;
                        local prop = PropM.combine(ME.user, "rob_attrib", "luck");
                        sourceLuck = PropM.apply(prop, sourceLuck);
                        -- 对方幸运，此时self.data已经被修改过
                        local targetLuck = self.data["luck"];
                        local moreLuck = sourceLuck - targetLuck;
                        local percent = math.modf((200 * sourceLuck - 80 * targetLuck) /
                                            (sourceLuck + 4 * targetLuck + 20));
                        detail = string.format(getLocStr("extra_scrap_detail"), moreLuck, percent / 10);
                    end
                    para = {["name"] = nameStr, ["detail"] = detail};
                end
            elseif goodsList[i][1] == 2 then
                if classId == "money" and i == 2 then
                    -- 矿车中的金币
                    goodsIcon = getRelicIcon("car_money");
                    local detail = getLocStr("car_money_detail");
                    para = {["detail"] = detail};
                else
                    goodsIcon = getLargeAttribIconPath(FieldsM.query(classId, "icon"));
                end
            elseif goodsList[i][1] == 6 then
                -- 秘宝
                goodsIcon = getAirShipSkillIconPath(SkyShipSkillM.query(classId, "icon"));
            elseif goodsList[i][1] == 8 then
                -- 机器人
                goodsIcon = getRobotIconPath(SkyRobotM.query(classId, "icon"));
            end

            iconImg:loadTexture(goodsIcon);

            -- 注册点击事件
            self:clickShowDesc(grid, goodsList[i], para);
        else
            -- 多余的格子置灰
            iconImg:setVisible(false);
            setGrayTransMode(goodsBg, true);
        end
    end
end

-- 点击图标处理
function UIRelicUserInfo:clickShowDesc(node, bonus, para)
    -- 点击处理
    local bg = findChildByName(node, "bg");
    bg:setTouchEnabled(true);
    bg:addTouchEventListener(function(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("workshop_material");
            -- 显示道具详细信息
            if bonus[1] == 1 then
                -- 物品
                UIMgr:getCurrentScene():removeFormByName("UIItemDesc");
                local uiItemDesc = UIItemDesc.create(bonus[2], nil, para);
                UIMgr:getCurrentScene():addForm(uiItemDesc);
            elseif bonus[1] == 2 and bonus[2] == "money" then
                -- TODO属性，暂时使用物品的，到时候需要和属性描述一起处理
                UIMgr:getCurrentScene():removeFormByName("UIItemDesc");
                local uiItemDesc = UIItemDesc.create(bonus[2], nil, para);
                UIMgr:getCurrentScene():addForm(uiItemDesc);
            elseif bonus[1] == 3 then
                -- 英雄
                UIMgr.getCurrentScene():removeFormByName("UIHeroDetailInfo");
                local uiHeroInfo = UIHeroDetailInfo.create(bonus[2]);
                UIMgr.getCurrentScene():addForm(uiHeroInfo);
            elseif bonus[1] == 6 then
                -- 秘宝
                UIMgr.getCurrentScene():removeFormByName("UIAirShipSkillDesc");
                local uishipSkillForm = UIAirShipSkillDesc.create(bonus[2]);
                UIMgr:getCurrentScene():addForm(uishipSkillForm);
            elseif bonus[1] == 8 then
                -- 机器人
                UIMgr:getCurrentScene():removeFormByName("UISkyStoreItem");
                require "game/ui/form/sky_city/UISkyStoreItem";
                local uiStoreItem = UISkyStoreItem.create(bonus[2], "robot", {["robot_tip"] = true});
                UIMgr.getCurrentScene():addForm(uiStoreItem);
            end
        end
    end);
end

-- 获取信息
function UIRelicUserInfo:getDetailInfo()
    if self.info[1] == "relic" then
        self.data = table.deepcopy(SkyRelicM.getTargetRobInfo(self.info[2]));

        local attribs = {"fire", "luck", "speed", "armor", "fight_ability"};
        -- 战力、四围修正
        for _, attrib in pairs(attribs) do
            -- 防守属性影响
            if self.data["guard_" .. attrib] then
                self.data[attrib] = self.data[attrib] + self.data["guard_" .. attrib];
            end
        end

        local skyProp = self.data["sky_prop"] or {};
        for _, attrib in ipairs(attribs) do
            local addon = SkyPropM.calcAddonByData({["sky_prop"] = skyProp}, attrib, self.data[attrib]);
            self.data[attrib] = self.data[attrib] + addon;
        end

        -- printTable(self.data, "relic");

    elseif self.info[1] == "boss" then
        self.data = SkyRelicM.getBossDetailInfo();
    end
end

-- 重绘界面
function UIRelicUserInfo:redraw()
    -- 重新获取信息
    self:getDetailInfo();

    local data = self.data;
    -- 显示标题
    local titleLabel = findChildByName(self.node, "Top/title/text");
    titleLabel:setString(getNickName(data.name));
    if self.info[1] == "relic" then
        -- 玩家名字使用系统字
        titleLabel:setFontSize(TextStyleM.TEXT_SIZE_MIDDLE);
        titleLabel:setTextColor(TextStyleM.TEXT_COLOR_BROWN_YELLOW);
    elseif self.info[1] == "boss" then
        -- BOSS使用游戏字
        TextStyleM.setArtWordTitleStyle(titleLabel);
    end

    -- 绘制图标
    local airShipNode = findChildByName(self.node, "CT/airship");
    airShipNode:stopAllActions();
    airShipNode:removeAllChildren();

    local towerNode = findChildByName(self.node, "CT/tower_node");
    towerNode:stopAllActions();
    towerNode:removeAllChildren();

    if self.info[1] == "relic" then
        -- 飞艇
        createAirShip(airShipNode, data.ship_id);

        if type(NewAirShipM.query(data.ship_id, "fly_effect")) == "number" then
            airShipNode:setPositionX(airShipNode:getPositionX());
        end

        -- 飞艇悬浮
        playFluctuateAnima(airShipNode, 0.1, 1);
    elseif self.info[1] == "boss" then
        local monsterId = self.data["pets"][1];
        require "game/ui/form/dungeon/UIGrid";
        -- 怪物模型
        local monsterDisplay = UIGrid:createMonsterDisplay(monsterId);
        monsterDisplay:setScale(0.95);
        -- 修正回正中
        towerNode:setPositionX(0);
        if self.initIconY == nil then
            self.initIconY = towerNode:getPositionY();
        end

        towerNode:setPositionY(self.initIconY + 5);

        towerNode:addChild(monsterDisplay);
    end

    -- 显示符石框
    local stoneNode = findChildByName(self.node, "BT2/stone");

    -- 没有增加的时候才增加
    if not stoneNode.iconNode then
        local itemNode = cc.CSLoader:createNode("layout/sky_city/SkyCityStoneItem.csb");
        stoneNode.iconNode = findChildByName(itemNode, "icon");
        stoneNode.iconNode:setVisible(false);
        stoneNode.bgNode = findChildByName(itemNode, "bg");
        stoneNode.hintNode = findChildByName(itemNode, "hint");

        itemNode:setScale(0.8);
        stoneNode:addChild(itemNode);

        -- 显示泡泡
        stoneNode.hintNode:setVisible(true);
        playCanEquipHintEffect(stoneNode.hintNode, 0, 0);
    end

    -- 如果掠夺目标为boss，不显示符石框
    if self.info[1] == "boss" then
        stoneNode:setVisible(false);
    end

    local detailNode = findChildByName(self.node, "CT/detail_node");
    -- 火力
    local attribLabel = findChildByName(detailNode, "fire_node/attrib");
    local valueLabel = findChildByName(detailNode, "fire_node/value");

    self:setSystemTextStyle(attribLabel);
    self:setSystemTextStyle(valueLabel);

    attribLabel:setString(getLocStr("airship_fire"));
    valueLabel:setString(data.fire);

    -- 装甲
    attribLabel = findChildByName(detailNode, "armor_node/attrib");
    valueLabel = findChildByName(detailNode, "armor_node/value");

    self:setSystemTextStyle(attribLabel);
    self:setSystemTextStyle(valueLabel);

    attribLabel:setString(getLocStr("airship_armor"));
    valueLabel:setString(data.armor);

    -- 机动
    attribLabel = findChildByName(detailNode, "speed_node/attrib");
    valueLabel = findChildByName(detailNode, "speed_node/value");

    self:setSystemTextStyle(attribLabel);
    self:setSystemTextStyle(valueLabel);

    attribLabel:setString(getLocStr("airship_speed"));
    valueLabel:setString(data.speed);

    -- 幸运
    attribLabel = findChildByName(detailNode, "luck_node/attrib");
    valueLabel = findChildByName(detailNode, "luck_node/value");

    self:setSystemTextStyle(attribLabel);
    self:setSystemTextStyle(valueLabel);

    attribLabel:setString(getLocStr("airship_luck"));
    valueLabel:setString(data.luck);

    -- 总战力
    attribLabel = findChildByName(detailNode, "ability_node/attrib");
    valueLabel = findChildByName(detailNode, "ability_node/value");

    self:setSystemTextStyle(attribLabel);
    self:setSystemTextStyle(valueLabel);

    attribLabel:setString(getLocStr("fight_ability_2"));

    -- 逗号分段战力
    local fightAbility = data.fight_ability;
    valueLabel:setString(changeNumString(fightAbility));

    -- 显示阵营
    local slimeLabel = findChildByName(detailNode, "camp_node/attrib");
    local countLabel = findChildByName(detailNode, "camp_node/value");

    self:setSystemTextStyle(slimeLabel);
    self:setSystemTextStyle(countLabel);

    -- 冈布奥
    if self.info[1] == "relic" then
        slimeLabel:setString(getLocStr("pets_camp"));
        local campId = CampM.getPetCamp(data.pets[1]);
        local campName = CampM.query(campId, "name");
        countLabel:setString(campName);
    elseif self.info[1] == "boss" then
        slimeLabel:setString(getLocStr("remain_fight_ability"));
        countLabel:setString(string.format("%0.1f%%", data["left_hp"] * 100/data["fight_ability"]));
    end

    -- 国籍
    local nation = findChildByName(self.node, "Top/title/nation");
    local nationId = data.nation or 0;
    if self.info[1] == "relic" then
        -- 显示国籍
        local icon = TopM.queryNationality(nationId, "icon");
        nation:loadTexture(getNationalityIconPath(icon));
        nation:setVisible(true);

        -- 先调整下位置
        titleLabel:setPositionX(self.titleLabelX);

        -- 国籍位置自适应
        setNextNodePosX(titleLabel, nation, 10);

        -- 获取下国籍宽度
        local nationWidth = nation:getContentSize().width;
        titleLabel:setPositionX(titleLabel:getPositionX() + nationWidth / 2);
        nation:setPositionX(nation:getPositionX() + nationWidth / 2);

        -- 国籍和
    elseif self.info[1] == "boss" then
        -- 隐藏
        nation:setVisible(false);
    end


    self:redrawBonus();

    self:redrawDesc();
end

-- 更新符石图标
function UIRelicUserInfo:updateStone(stoneClassId)
    self.stoneClassId = stoneClassId;

    local stoneNode = findChildByName(self.node, "BT2/stone");
    local iconName = ItemM.query(stoneClassId, "icon");
    stoneNode.iconNode:setVisible(true);
    stoneNode.iconNode:loadTexture(getItemIconPath(iconName));
    stoneNode.hintNode:setVisible(false);
end

-- 绘制BOSS离开时间
function UIRelicUserInfo:redrawBossTime(timeLPosx)
    local subheadLabel = findChildByName(self.node, "BT2/desc_node/time_label");
    local kerning = -3;
    if not isFullWidthLang() then
        kerning = -1;
    end
    TextStyleM.setTextStyle(subheadLabel, TextStyleM.TEXT_SIZE_SMALL,
        TextStyleM.TEXT_COLOR_GRAY, false, getLocKerning() + kerning);

    subheadLabel:setPositionX(timeLPosx);

    subheadLabel:stopAllActions();

    if self.info[1] ~= "boss" then
        subheadLabel:setVisible(false);
        return;
    end

    subheadLabel:setVisible(true);
    -- BOSS只存在24小时
    local function tick()
        local leftTime = self.data["last_appear_time"] + 24 * 3600 - TimeM.getServerTime();
        if leftTime <= 0 then
            -- BOSS失效了,从位置信息中删除
            SkyRelicM.deleteRelicPosInfoByType(self.info[1], self.info[2]);
            -- 抛出刷新事件
            EventMgr.fire(event.RELIC_REFRESH_MAP, {});
            -- 弹字
            alert(string.format(getLocStr("relic_boss_leave_tip"), self.data["name"]));
            -- 关闭自身界面
            UIMgr.getCurrentScene():removeFormByName("UIRelicUserInfo");
            return;
        end
        subheadLabel:setString(remainingTimeDesc3(leftTime, "leave"));
        performWithDelay(subheadLabel, tick, 1);
    end
    tick();
end

-- 注册事件处理回调函数
function UIRelicUserInfo:registerEventCallback()
    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            EventMgr.removeAll("UIRelicUserInfo");
        elseif eventType == "enter" then
            -- 尝试刷新信息
            SkyRelicM.refreshRelicInfos();
        end
    end);

    -- 注册获得焦点的回调
    EventMgr.register("UIRelicUserInfo", event.FOCUS_CHANGED, function(para)
        if para["get"] == "UIRelicUserInfo" then
            UIMgr:getCurrentScene().topMenu:show(TOPMENU_FRAME_AIRSHIP_SCORE, TOPMENU_FRAME_AIRSHIP_ATTRIB, TOPMENU_FRAME_HEAD_AIRSHIP);
        elseif para["lose"] == "UIRelicUserInfo" then

        end
    end);

    -- 注册天空之城战斗的回调
    EventMgr.register("UIRelicUserInfo", event.START_SKY_BATTLE, function(para)
        -- 关闭 "加载中" 界面
        closeCommunicatingForm();
        -- 直接去掉界面
        UIMgr.getCurrentScene():removeFormByName("UIRelicUserInfo");
    end);

    -- 掠夺列表更新了
    EventMgr.register("UIRelicUserInfo", event.RELIC_CHANGE_TARGET, function(para)
        local pos = para.pos;
        if self.info[1] == "relic" and pos == tonumber(self.myPos) then
            -- 直接关闭自身
            UIMgr.getCurrentScene():removeFormByName("UIRelicUserInfo");
        end
    end);

    -- 掠夺信息更新
    EventMgr.register("UIRelicUserInfo", event.RELIC_ROB_LIST_UPDATE, function(para)
        if self.info[1] == "relic" then
            -- 重绘界面
            self:redraw();
            self:redrawTime();
        end
    end);

    -- BOSS信息更新
    EventMgr.register("UIRelicUserInfo", event.RELIC_BOSS_INFO_REFRESH, function(para)
        if self.info[1] == "boss" then
            -- 重绘界面
            self:redraw();
            self:redrawTime();
        end
    end);
end

-- 注册点击事件
function UIRelicUserInfo:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("ship_return");
            UIMgr.getCurrentScene():removeFormByName("UIRelicUserInfo");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册掠夺按钮
    local btnAttack = findChildByName(self.node, "BT/btn_make");
    local function onAttackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 判断是否在冷却时间
            if SkyRelicM.isInCoolTime(self.data, self.info[1]) then
                self:showCoolTimeTip();
                return;
            end

            wait(2);

            local function delay()
                -- 2s还没收到，就卡住
                showCommunicatingForm(getLocStr("loading"));
            end
            performWithDelay(self.node, delay, 2);

            if self.info[1] == "relic" then
                -- 发起挑战
                SkyRelicM.robRelic(self.data["rid"], 0, self.stoneClassId or 0);
            elseif self.info[1] == "boss" then
                -- 挑战BOSS
                SkyRelicM.strikeRelicBoss();
            end
        end
    end
    btnAttack:addTouchEventListener(onAttackClick);
    TextStyleM.setTextStyle(btnAttack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);

    -- 时间函数tick
    self:redrawTime();

    -- 注册替换按钮
    local btnActived = findChildByName(self.node, "BT/btn_actived");
    local function onChangeClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 更换目标
            SkyRelicM.changeRelicTarget(self.data["rid"]);
        end
    end
    btnActived:addTouchEventListener(onChangeClick);
    TextStyleM.setTextStyle(btnActived, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnActived:setTitleText(getLocStr("btn_text_change_target"));

    local function tick()
        -- 检查按钮显示
        self:checkChangeBtn();
        if self.info[1] == "relic"
            -- 5s检查一次
            and not SkyRelicM.canChangeTarget(self.info[2]) then
            performWithDelay(self.node, tick, 5);
        end
    end
    tick();

    -- 隐藏翻页按钮
    local btnNext = findChildByName(self.node, "CT/btn_next");
    local btnLast = findChildByName(self.node, "CT/btn_last");
    local hideNodes = {btnNext, btnLast};
    for _, node in pairs(hideNodes) do
        node:setVisible(false);
    end

    -- 符石框点击处理
    local stoneNode = findChildByName(self.node, "BT2/stone");
    stoneNode.bgNode:setTouchEnabled(true);
    local function onStoneImgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 打开符石选择框
            require "game/ui/form/sky_city/UISkyStoneChoose";
            UIMgr.getCurrentScene():removeFormByName("UISkyStoneChoose");
            local uiForm = UISkyStoneChoose.create("plunder", self);
            UIMgr.getCurrentScene():addForm(uiForm);
        end
    end
    stoneNode.bgNode:addTouchEventListener(onStoneImgClick);

    -- -- 身上没有符石的话需要显示泡泡
    -- local hasStone = SkyStoneM.hasStone("plunder");
    -- if hasStone then
    --     stoneNode.hintNode:setVisible(false);
    -- else
    --     stoneNode.hintNode:setVisible(true);
    --     playCanEquipHintEffect(stoneNode.hintNode, 0, 0);
    -- end
end

-- 检查是否显示更换目标按钮
function UIRelicUserInfo:checkChangeBtn()
    local btnActived = findChildByName(self.node, "BT/btn_actived");
    if self.info[1] == "boss" then
        -- BOSS信息则隐藏
        btnActived:setVisible(false);
    elseif self.info[1] == "relic" then
        btnActived:setVisible(true);
        -- 判断当前能否更换
        if SkyRelicM.canChangeTarget(self.info[2]) then
            setButtonEnabled(btnActived, true);
        else
            setButtonEnabled(btnActived, false);
        end
    end
end

-- 按钮时间回调
function UIRelicUserInfo:redrawTime()
    local btnAttack = findChildByName(self.node, "BT/btn_make");
    -- 时间回调/重新获取时间
    self.robWaitTime = FormulaM.invoke("CALC_ROB_FAIL_WAIT_TIME", self.data["fail_times"], self.info[1] == "boss");

    if self.isTick then
        return;
    end

    local function tick()
        -- 上次掠夺时间
        local lastTime = self.data["last_fail"] or 0;
        local leftTime = lastTime + self.robWaitTime - TimeM.getServerTime();

        if leftTime > 0 then
            -- 显示剩余时间
            btnAttack:setTitleText(remainingTimeDesc(leftTime));
            performWithDelay(self, tick, 1);
        else
            -- 直接显示按钮
            if self.info[1] == "relic" then
                btnAttack:setTitleText(getLocStr("btn_text_punder"));
            elseif self.info[1] == "boss" then
                btnAttack:setTitleText(getLocStr("btn_text_strike"));
            end
            self.isTick = false;
        end
    end

    self.isTick = true;
    tick();
end

-- 显示立即掠夺提示框
function UIRelicUserInfo:showCoolTimeTip()
    local cost = FormulaM.invoke("CALC_NO_WAIT_TO_ROB_COST");

    -- 确认函数
    local function okFun()
        if ME.user.dbase:query(cost[2], 0) < cost[3] then
            -- 弹出购买钻石提示
            showGemLackConfirm();
            return;
        else
            wait(2);

            if self.info[1] == "relic" then
                SkyRelicM.robRelic(self.data["rid"], 1, self.stoneClassId or 0);
            elseif self.info[1] == "boss" then
                SkyRelicM.strikeRelicBoss(1);
            end
        end
    end

    local robWaitTime = FormulaM.invoke("CALC_ROB_FAIL_WAIT_TIME", self.data["fail_times"], self.info[1] == "boss");
    local lastTime = self.data["last_fail"];
    local finalTime = lastTime + robWaitTime;
    local leftTime = finalTime - TimeM.getServerTime();

    local desc;
    if self.info[1] == "relic" then
        desc = string.format(getLocStr("last_revenge_fail_tip"), remainingTimeDesc(leftTime));
    elseif self.info[1] == "boss" then
        desc = string.format(getLocStr("last_revenge_fail_tip"), remainingTimeDesc(leftTime));
    end

    local title;
    if self.info[1] == "relic" then
        title = getLocStr("rob_tip_title");
    elseif self.info[1] == "boss" then
        title = getLocStr("rob_tip_title");
    end

    -- 弹出离开迷宫确认提示框
    confirm2(CONFIRM_TYPE_OK_CANCEL, title, desc,
        okFun, nil, getLocStr("btn_text_rob_now"), getLocStr("btn_text_cancel"),
        { ["cancelColor"] = "blue", ["leftTimeDesc"] = {self.info[1], finalTime} });
end

-- 适配
function UIRelicUserInfo:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");
    AlignM.fitToScreen(node, "bg2");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    AlignM.alignToTopCenter(node, "Top");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
    AlignM.alignToBottomCenter(node, "BT2");
end

-- 设置信息的字体
function UIRelicUserInfo:setSystemTextStyle(textLabel)
    textLabel:setFontSize(TextStyleM.TEXT_SIZE_TINY);
    textLabel:setTextColor(TextStyleM.TEXT_COLOR_BRIGHT_BLUE);
    TextStyleM.setOutlineStyle(textLabel, 2, TextStyleM.TEXT_COLOR_DARK_BLUE2, true);
end

-- 根据当前抽取到的类型，重绘描述信息
function UIRelicUserInfo:redrawDesc()
    local descNode = findChildByName(self.node, "BT2/desc_node");
    local titleLabel = findChildByName(descNode, "title_label");
    local descLabel = findChildByName(descNode, "desc_label");

    local titleName;
    local descStr;
    if self.info[1] == "relic" then
        -- 标题为飞艇名称
        titleName = NewAirShipM.query(self.data.ship_id, "name");
        -- 掠夺描述
        descStr = getLocStr("relic_airship_desc");
    else
        titleName = self.data.name;
        descStr = getLocStr("relic_boss_desc");
    end

    local lineCount = 4;
    local countPerLine = TextStyleM.TEXT_WORD_COUNT_PER_LINE;
    local fontSize = TextStyleM.TEXT_SIZE_SMALL;
    if isFullWidthLang() then
        lineCount = 3;
    elseif getLang() == "th" then
        lineCount = 1.4;
        fontSize = TextStyleM.TEXT_SIZE_MINI;
    else
        countPerLine = 24;
    end

    if getLang() == "ja" or getLang() == "ko" then
        fontSize = TextStyleM.TEXT_SIZE_SMALL2;
        countPerLine = 24;
    end
    local textScale = TextStyleM.getTextScale(descStr, countPerLine * 2 * lineCount);
    TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    TextStyleM.setTextStyle(descLabel, fontSize, TextStyleM.TEXT_COLOR_DARK_BROWN, nil, nil, nil, nil, countPerLine, nil, nil, textScale);
    titleLabel:setString(getNickName(titleName));
    descLabel:setString(descStr);

    local titlePosX = titleLabel:getPositionX();
    local labelWidth = titleLabel:getContentSize().width;

    local timeLPosx = titlePosX + labelWidth + 20;
    -- 绘制BOSS离开时间
    self:redrawBossTime(timeLPosx);
end


-- 从中间浮现效果
function UIRelicUserInfo:comformCenterEffect()
    local TOP = findChildByName(self.node, "Top");
    local BT2  = findChildByName(self.node, "BT2");
    if not TOP then
        return;
    end

    local titleNode = findChildByName(TOP, "title");
    local descNode = findChildByName(BT2, "desc_node");

    local oldScale = titleNode:getScale();
    local descSale = descNode:getScale();

    -- title
    titleNode:setScale(oldScale * 0.8);
    local scaleUp1 = cc.EaseIn:create(cc.ScaleTo:create(0.2, oldScale * 1.05), 0.4);
    local scaleUp2 = cc.ScaleTo:create(0.05, oldScale);
    titleNode:runAction(cc.Sequence:create(scaleUp1, scaleUp2));

    descNode:setScale(descSale * 0.8);
    local scaleUp3 = cc.EaseIn:create(cc.ScaleTo:create(0.2, descSale * 1.05), 0.4);
    local scaleUp4 = cc.ScaleTo:create(0.05, descSale);
    descNode:runAction(cc.Sequence:create(scaleUp3, scaleUp4));
end